<template>
  <div class="cont" v-show="contShow">
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="cont_top" label-width="50px"
      @submit.prevent="handleQuery">
      <el-form-item label="活动主题" prop="topic" style="margin-right:17px;" label-width="74px">
        <el-input v-model="queryParams.topic" placeholder="" @keyup.enter="handleQuery" style="width: 200px;" />
      </el-form-item>
      <el-form-item style="margin-right:0;">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="cont_top" label-width="50px">
      <el-form-item style="margin-right:0;">
        <el-button link type="primary" @click="addbtn" class="btnhover"
          style="width: 90px;height: 30px;background-color: #FF9900;color: rgba(255,255,255,1);">新增</el-button>
        <el-button link type="primary" @click="alldelbtn" class="btnhover"
          style="width: 90px;height: 30px;background-color: #FF9900;color: rgba(255,255,255,1);">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" element-loading-text="Loading..." :data="list"
      style="margin-bottom: 20px;min-height: 435px;" :header-cell-style="{ background: '#F0F0F0' }"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" />
      <el-table-column align="center" show-overflow-tooltip label="活动主题" prop="topic" min-width="300">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.topic }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="封面图" prop="coverPhotoUrl" min-width="120">
        <template #default="scope">
          <el-image v-if="scope.row.coverPhotoUrl" style="width:60px;height:55px;" :src="scope.row.coverPhotoUrl" />
          <div v-else style="width:60px;height:55px;line-height:55px;">暂无图片</div>
        </template>
      </el-table-column>
      <el-table-column :formatter="dateFormatter2" align="center" label="活动开始时间" prop="beginTime" min-width="200" />
      <el-table-column :formatter="dateFormatter2" align="center" label="活动结束时间" prop="endTime" min-width="200" />
      <el-table-column align="center" show-overflow-tooltip label="提交人" prop="creator" min-width="160">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.creator }}
          </div>
        </template>
      </el-table-column>
      <el-table-column :formatter="dateFormatter" align="center" label="提交时间" prop="createTime" min-width="200" />
      <el-table-column align="center" label="操作" fixed="right" width="140">
        <template #default="scope">
          <el-button link type="primary" class="btnhover" @click="detailsbtn(scope.row)"
            style="color: #3C88F0;">详情</el-button>
          <el-button link type="primary" class="btnhover" @click="editbtn(scope.row)"
            style="color: #3C88F0;">编辑</el-button>
          <el-button link type="primary" class="btnhover" @click="delbtn(scope.row)"
            style="color: #3C88F0;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination style="font-size: 14px;" v-model:current-page="queryParams.pageNo" :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize" :page-sizes="pageSizes" layout="total,sizes,prev, pager, next,jumper"
      :total="total" @size-change="selectchange" @current-change="handleCurrentChange" />
  </div>
  <AddDialog v-show="addShow" :addShow="addShow" @getaddShow="getaddShow" :editData="editData"
    @geteditData="geteditData" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { dateFormatter2 } from '@/utils/formatTime'
import { dateFormatter } from '@/utils/formatTime'
import AddDialog from './components/addDialog.vue'
import * as getTouristApi from '@/api/smartTourism/index'
const { push } = useRouter() // 路由
const selectList = ref([]) // 表格选中数据
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [10, 20, 50, 100]
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  topic: '',
})
const queryFormRef = ref() // 搜索的表单
const addShow = ref(false)
const contShow = ref(true)
const editData = ref({})
const getaddShow = (val) => {
  addShow.value = val
  contShow.value = !val
}
const geteditData = (val) => {
  handleCurrentChange(val)
}
/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

/** 查询任务列表 */
const getList = async () => {
  getTouristApi.getActivityPage(queryParams).then((res) => {
    let data = reactive(res.list)
    list.value = data;
    total.value = res.total
    if (list.value.length === 0) {
      value.value = true
    } else {
      value.value = false
    }
    loading.value = false
  }).catch(error => {
    loading.value = false
  })
}

/** 详情 */
const detailsbtn = (row: any) => {
  push({
    name: 'activeDetails',
    query: {
      id: row.id
    }
  })
}

/** 新增 */
const addbtn = () => {
  addShow.value = true
  contShow.value = false
  editData.value = {}
  editData.value.dialogtitle = '新增图文'
  editData.value.pageNo = 1
}

/** 编辑 */
const editbtn = (row: any) => {
  addShow.value = true
  contShow.value = false
  editData.value = row
  editData.value.dialogtitle = '编辑图文'
  editData.value.pageNo = queryParams.pageNo
}

/** 批量删除 */
const handleSelectionChange = (e) => {
  selectList.value = e;
}
const alldelbtn = () => {
  if (selectList.value.length == 0) {
    ElMessage({
      message: '至少选中一条数据！',
      type: 'warning'
    })
    return
  }
  ElMessageBox.confirm(
    '此操作将永久删除选中活动, 是否继续?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const idList = reactive([])
      selectList.value.forEach((item) => {
        idList.push(item.id)
      })
      getTouristApi.deleteActivityList(idList).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          // setTimeout(() => {
          getList()
          // }, 1000)
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}


/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm(
    '此操作将永久删除该活动, 是否继续?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      getTouristApi.deleteActivityList([row.id]).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          // setTimeout(() => {
          getList()
          // }, 1000)
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}


/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  min-height: 557px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #CC352F;
}

::v-deep .el-range-editor.is-active {
  box-shadow: 0 0 0 1px #CC352F;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #CC352F;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btn:hover {
  opacity: 0.8;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #CC352F;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #CC352F;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.star_style {
  font-size: 14px;
  color: #C0790F;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}

.namecont {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>